package com.shixun.dao;

import com.shixun.entity.FileEntity;
import com.shixun.entity.FpinglunEntity;
import com.shixun.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by haiyan on 2016/12/12.
 */
@Repository("ZhyDao")
public class ZhyDaoImpl extends BaseDao implements ZhyDao {

    @Autowired
    private FpinglunEntity fpinglunEntity;
    /**
     * 保存文章内容
     * @param fileEntity
     * @return
     */
    @Override
    public void saveFileDao(FileEntity fileEntity) {
        System.out.println("111" + fileEntity.toString());
        userAdd(fileEntity);
    }

    /**
     * 查找所有的文章
     * @return
     */
    @Override
    public List<Object> searchFileDao() {
        return userSelect("from FileEntity where more1='"+ "1" + "'");
    }

    /**
     * 通过id查找一条文章信息
     * @param str
     * @return
     */
    @Override
    public List<Object> searchOneDao(String str) {
        return userSelect("from FileEntity where id='" + str + "'");
    }

    /**
     * 通过id查找一条信息在   评论表里面
     * @param string
     * @return
     */
    @Override
    public List<Object> searchOneDaoFromPinLun(String string) {
        return userSelect("from FpinglunEntity where fileEntity.id ='" + string + "'");
    }

    /**
     * 存储点赞信息
     * @param string
     */
    @Override
    public void saveDianZanInfo(String string) {
        fpinglunEntity.setMore1("10");   //对文章的点赞的 区分
        fpinglunEntity.setFileEntity((FileEntity)searchOneDao(string).get(0));
        fpinglunEntity.setUserEntity((UserEntity)getSessions().get("user"));
        fpinglunEntity.setState("1");
        fpinglunEntity.setFtime(new Timestamp(System.currentTimeMillis()));
        fpinglunEntity.setCreatetime(new Timestamp(System.currentTimeMillis()));
        userAdd(fpinglunEntity);
    }

    /**
     * 存储文章的留言
     * @param
     * @param
     */
    @Override
    public void savePingLunInfo(String wzId, String lyMain) {
        fpinglunEntity.setMore1("11");   //对文章的留言区分
        fpinglunEntity.setFmain(lyMain);
        fpinglunEntity.setFileEntity((FileEntity) searchOneDao(wzId).get(0));
        fpinglunEntity.setUserEntity((UserEntity) BaseDao.getSessions().get("user"));
        fpinglunEntity.setFtime(new Timestamp(System.currentTimeMillis()));
        fpinglunEntity.setCreatetime(new Timestamp(System.currentTimeMillis()));
        fpinglunEntity.setState("1");
        userAdd(fpinglunEntity);
    }

    /**
     * 查询文章的留言信息
     * @param wzId
     * @return
     */
    @Override
    public List<Object> selectWenZhangInfo(String wzId) {
        return userSelect("from FpinglunEntity where fileEntity.id ='" + wzId + "'");
    }

    /**
     * 查找文件表  以浏览量来排序
     * @return
     */
    @Override
    public List<Object> selectWenZhangSix() {    //不要用sql去检验hql的正确性   因为检验也是没用
        return userSelect("from FileEntity t order by cast(t.more2 as integer) DESC");
    }

    /**
     * 通过用户id查找所有该用户文件（fileEntity）
     */
    @Override
    public List<Object> selectAllFile(String string) {
        return userSelect("from FileEntity where userEntity.id='" + string + "'");
    }

    /**
     * 通过文件id删除  一条文件信息
     * @param fileEntity
     */
    @Override
    public void delteFile(FileEntity fileEntity) {
        deleteId("DELETE FROM FileEntity WHERE id = '" + fileEntity.getId() + "'");
    }

    /**
     * 差找所有的用户
     */
    @Override
    public List<Object> selectAllUser() {
        return userSelect("from UserEntity");
    }

    /**
     * 通过用户id编号查找用户
     */
    @Override
    public List<Object> select(String string) {
        return userSelect("from UserEntity where id='" + string + "'");
    }

    /**
     * 查找所有帖子
     * @return
     */
    @Override
    public List<Object> searchTieziDao() {
        return userSelect("from FileEntity where more1='"+ "0" + "'");
    }
}
